require("./index.scss");
var ParseTemplate = ZFT.Util.ParseTemplate;
var Ajax = ZFT.Util.Ajax;
var STip = ZFT.Util.STip;
var Template = {
    note: ParseTemplate(require("./tpl/note.xtpl"))
}

var Main = ZFT.Util.Class({
    container: $("body"),
    init: function () {
        var _this = this;
        _this.loadTpl();
    },
    EVENTS: {
        "click #printBtn": "printNote"
    },
    loadTpl: function () {
        var _this = this;
        var regUrl = /^\d+$/;
        var param;
        var html;
        var url = window.location.href;
        var urlParam = ZFT.Util.UrlParse(url);
        var type = urlParam["type"];
        var beginDate = urlParam["begin"];
        var endDate = urlParam["end"];
        var orderNum = urlParam["num"];
        var tpl
        //var url = { 1: "/r/BillManager_getPayNote" }
        if (!regUrl.test(type) || !regUrl.test(beginDate) || !regUrl.test(endDate) || !regUrl.test(orderNum) || !url[type]) {
            return STip("fail", "参数错误！3秒后将关闭窗口", 3000, function () {
                window.close();
            })
        }
        param = {
            ordernum: orderNum,
            begin_cycle: beginDate,
            end_cycle: endDate,
            type: type
        }
        Ajax("/r/BillManager_getPayNote", {
            type: "POST",
            dataType: "json",
            params: param,
            success: function (res) {
                if (res.code == 200) {
                    var data = res.data;
                    var newBeginDate = _this.splitDate(data.begin_cycle),
                        newEndDate = _this.splitDate(data.end_cycle);

                    data["nbegin"] = newBeginDate;
                    data["nend"] = newEndDate;
                    data["type"]=type;
                    if (type == 2) {
                        var newDate = new Date(),
                            timestamp = Date.parse(newDate) / 1000,
                            fullYear = newDate.getFullYear(),
                            randomSecond = String(timestamp).substr(6, 4),
                            randomNo = fullYear + randomSecond;
                        data["feewater"] = _this.splitMoney(data.water.money + '');
                        data["feepower"] = _this.splitMoney(data.power.money + '');
                        data["feerent"] = _this.splitMoney(data.rent + '');
                        data["randomNo"] = randomNo;
                        data["paytime"] = _this.splitDate(data.pay_time.replace(/[\-]/g, ""))
                        data["digmoney"] = _this.digitUppercase(data.total);
                    }
                    //console.log(data);
                    html = Template.note({ data: data });
                    $("#content").html(html);
                } else {
                    return STip("fail", res.msg + "! 3秒后将关闭窗口！", 3000, function () {
                        window.close();
                    })
                }
            }

        })

    },
    //过滤年月日
    splitDate: function (dates) {
        var y = dates.substr(0, 4),
            m = dates.substr(4, 2),
            d = dates.substr(6, 2),
            newdate;
        return newdate = {
            y: y,
            m: m,
            d: d
        }
    },
    //拆分价格
    splitMoney: function (money) {
        var arr = [],
            len,
            length;

        //   console.log(money)
        if (money.length < 1 || money == "undefined") return ['', '', '', '', '', '', ''];
        money = money.replace(/[\.]/g, "");
        len = money.length;
        length = (len > 6) ? 6 : len;
        for (var i = 1; i <= length; i++) {
            arr.push(money.substr(-i, 1));
        }
        for (var i = 0; i < 7 - len; i++) {
            arr.push('');
        }
        if (len > 6) {
            var tth = money.substr(0, len - 6); //截取万为单位的数字
            arr.push(tth);
        }
        return arr.reverse();
    },
    //打印事件
    printNote: function (e) {
        var tarBtn = $(e.currentTarget),
            _this = this;
        tarBtn.hide();
        window.print();
        tarBtn.show();
    },



    digitUppercase: function (n) {
        var fraction = ['角', '分'];
        var digit = [
            ' 零', ' 壹', ' 贰', ' 叁', ' 肆',
            ' 伍', ' 陆', ' 柒', ' 捌', ' 玖'
        ];
        var unit = [
            ['元', ' 万', '亿'],
            ['', '拾', '佰', '仟']
        ];
        n = Math.abs(n);
        var s = '';
        for (var i = 0; i < fraction.length; i++) {
            s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
        }
        s = s || '整';
        n = Math.floor(n);
        for (var i = 0; i < unit[0].length && n > 0; i++) {
            var p = '';
            for (var j = 0; j < unit[1].length && n > 0; j++) {
                p = digit[n % 10] + unit[1][j] + p;
                n = Math.floor(n / 10);
            }
            s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
        }
        return s.replace(/(零.)*零元/, '元')
            .replace(/(零.)+/g, '零')
            .replace(/^整$/, '零元整');
    }


});

$(function () {
    new Main();
})